package com.example.jl.bethinner.activity;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.example.jl.bethinner.R;
import com.example.jl.bethinner.adapter.OpenStoreListAdapter;
import com.example.jl.bethinner.domain.City;
import com.example.jl.bethinner.domain.Province;
import com.example.jl.bethinner.fragment.CityChooserDialog;
import com.example.jl.bethinner.fragment.ProvinceChooserDialog;
import com.example.jl.bethinner.util.GaussianBlurFilter;
import com.example.jl.bethinner.util.processImageTask;

import java.util.ArrayList;
import java.util.List;

public class EditListDemoActivity extends ActionBarActivity implements OpenStoreListAdapter.OpenStoreCityChooser, ProvinceChooserDialog.OnProvinceSelectedLisenter, CityChooserDialog.OnCitySelectedLisenter {

    private ListView listView;
    OpenStoreListAdapter openStoreListAdapter;
    private List<String> descriptions;
    private List<String> names;
    private Province selected_province;
    private City selected_city;
    private ImageView mMainHeader;
    private int mMinHeaderTranslation;
    private int mActionBarSize;
    private RelativeLayout mListViewHeader;
    private processImageTask mProcessImageTask;
    ProvinceChooserDialog provinceChooserDialog;
    CityChooserDialog cityChooserDialog;
    private AccelerateDecelerateInterpolator mSmoothInterpolator;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_list_demo);
        getActionBarSize();
        mSmoothInterpolator = new AccelerateDecelerateInterpolator();
        listView = (ListView) findViewById(R.id.listView);
        mMainHeader = (ImageView) findViewById(R.id.id_main_header);
        mListViewHeader = (RelativeLayout) getLayoutInflater().inflate(R.layout.actionbar_item_header, null);
        names = getNames();
        descriptions = getDescriptions();
        openStoreListAdapter = new OpenStoreListAdapter(this, names, descriptions, false);

        listView.addHeaderView(mListViewHeader);
        listView.setAdapter(openStoreListAdapter);
        listView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

                int scrollY = getScrollY();
                mMainHeader.setTranslationY(Math.max(-scrollY, mMinHeaderTranslation));
                Log.v("zgy", "======mMainHeader.getTranslationX()========" + mMainHeader.getTranslationY());
                float alpha = clamp(mMainHeader.getTranslationY() / mMinHeaderTranslation, 0.0f, 1.0f);
                Log.v("zgy", "======alpha========" + alpha);
                float actual = clamp(1.0f - mSmoothInterpolator.getInterpolation(alpha), 0.0f, 1.0f);
                mMainHeader.setAlpha(1.0f - actual);
            }
        });
        mMainHeader.post(new Runnable() {
            @Override
            public void run() {
                /*获取滚动的最小值*/
                mMinHeaderTranslation = -mMainHeader.getMeasuredHeight() + mActionBarSize;
                Log.v("zgy", "===========mMainHeader===" + mMainHeader.getMeasuredHeight());
            }
        });
        mProcessImageTask = new processImageTask(this, new GaussianBlurFilter());
        mProcessImageTask.setCallBack(new processImageTask.CallBack() {
            @Override
            public void complete(Bitmap bitmap) {
                mMainHeader.setImageBitmap(bitmap);
            }
        });
        mProcessImageTask.execute();

    }

    /**
     * 获取滚动的高度，用于检测是否需要滚动
     *
     * @return
     */
    private int getScrollY() {
        int scrollY = 0;
        int itemScrollY = 0;
        int itemNum = listView.getFirstVisiblePosition();
        View firstVisible = listView.getChildAt(0);
        if (firstVisible == null) {
            return scrollY;
        }
        if (itemNum >= 1) {
            itemScrollY = mListViewHeader.getMeasuredHeight();
        }
        scrollY = itemScrollY - firstVisible.getTop();
        return scrollY;
    }

    /**
     * 这个方法还是比较有用的，
     *
     * @param value
     * @param min
     * @param max
     * @return
     */
    public static float clamp(float value, float min, float max) {
        return Math.max(Math.min(value, max), min);
    }

    public List<String> getDescriptions() {
        descriptions = new ArrayList<>();
        descriptions.add("");
        descriptions.add("");
        descriptions.add("");
        descriptions.add("选择");
        descriptions.add("");
        descriptions.add("");
        descriptions.add("15869149722");
        descriptions.add("");
        descriptions.add("");
        descriptions.add("");
        descriptions.add("");
        descriptions.add("选择");
        descriptions.add("");
        descriptions.add("");
        descriptions.add("15869149722");
        descriptions.add("");


        return descriptions;
    }

    public List<String> getNames() {
        names = new ArrayList<>();
        names.add("店铺名称");
        names.add("店铺介绍");
        names.add("联系人");
        names.add("所在城市");
        names.add("所在位置");
        names.add("电话号码");
        names.add("手机号");
        names.add("验证码");
        names.add("店铺名称");
        names.add("店铺介绍");
        names.add("联系人");
        names.add("所在城市");
        names.add("所在位置");
        names.add("电话号码");
        names.add("手机号");
        names.add("验证码");


        return names;
    }

    @Override
    public void citySelected(City city) {
        selected_city = city;
        openStoreListAdapter.setDescription(3, selected_province.getProvince() + selected_city.getCity());
        openStoreListAdapter.notifyDataSetChanged();
    }

    @Override
    public void provinceSelected(Province provinces) {
        selected_province = provinces;
        cityChooserDialog = CityChooserDialog.newInstance(selected_province.getProvinceid());
        cityChooserDialog.show(getSupportFragmentManager(), "city");
    }

    @Override
    public void openStoreCityChooser() {
        if (null == selected_province) {
            if (null == provinceChooserDialog) {
                //未生成城市选择dialog
                provinceChooserDialog = ProvinceChooserDialog.newInstance();
            }
            provinceChooserDialog.show(getSupportFragmentManager(), "province");
        } else {
            cityChooserDialog.show(getSupportFragmentManager(), "city");
        }

    }

    /**
     * 测量ActionBar的高度
     */
    private void getActionBarSize() {
        TypedValue typedValue = new TypedValue();
        getTheme().resolveAttribute(R.attr.actionBarSize, typedValue, true);
        mActionBarSize = TypedValue.complexToDimensionPixelSize(typedValue.data, getResources().getDisplayMetrics());
        Log.v("zgy", "=============actionBarSize=" + mActionBarSize);
    }
}
